<?php

use Illuminate\Support\Facades\Storage;

if (! function_exists('if_route')) {
    /**
     * 比较当前路由和指定路由
     *
     * @param  string|array $route_name
     * @return boolean
     */
    function if_route($route_name)
    {
        return request()->routeIs($route_name);
    }
}

if (! function_exists('active_class')) {
    /**
     * 根据判断结果返回激活样式
     *
     * @param  boolean $condition
     * @param  string  $class_name
     * @return string
     */
    function active_class($condition, $class_name = 'active')
    {
        return $condition ? $class_name : '';
    }
}

if (! function_exists('active_menu')) {
    /**
     * 当前访问为该导航菜单页面则返回激活样式
     *
     * @param  string|array $route_name
     * @param  string $class_name
     * @return string
     */
    function active_menu($route_name, $class_name = 'active')
    {
        return active_class(if_route($route_name));
    }
}

if (! function_exists('whether')) {
    /**
     * 返回是或否
     *
     * @param  integer $value
     * @return string
     */
    function whether($value)
    {
        if ($value === 1) {
            return '是';
        }

        return '否';
    }
}

if (! function_exists('fill_zero')) {
    /**
     * 数字补零
     *
     * @param  integer $num
     * @return string
     */
    function fill_zero($num)
    {
        return (string) ($num < 10 ? '0' . $num : $num);
    }
}

if (! function_exists('upload_token')) {
    /**
     * 生成七牛上传token
     *
     * @param  integer|string $size_limit
     * @param  string  $mime_limit
     * @return string
     */
    function upload_token($size_limit = null, $mime_limit = null)
    {
        $body = [
            'file_name'   => '$(key)',
            'file_size'   => '$(fsize)',
            'file_type'   => '$(mimeType)',
            'file_path'   => 'http://' . env('QINIU_DEFAULT_DOMAIN') . '/$(key)',
            'source_name' => '$(fname)',
        ];

        $policy = [
            'scope' => '<bucket>:<key>',
            'returnBody' => json_encode($body),
        ];

        if ($size_limit) {

            if (! is_numeric($size_limit)) {

                $size_limit = strtoupper($size_limit);

                if (preg_match('/^(\d+)\s*(B|KB|MB|GB)$/', $size_limit, $matches) !== 1) {
                    return '';
                }

                $value = $matches[1];
                $unit = $matches[2];

                if ($unit === 'B') {
                    $size_limit = $value;
                } elseif ($unit === 'KB') {
                    $size_limit = $value * 1024;
                } elseif ($unit === 'MB') {
                    $size_limit = $value * pow(1024, 2);
                } elseif ($unit === 'GB') {
                    $size_limit = $value * pow(1024, 3);
                }
            }

            $policy['fsizeLimit'] = $size_limit;
        }

        if ($mime_limit) {
            $policy['mimeLimit'] = $mime_limit;
        }

        return Storage::uploadToken(null, 7200, $policy);
    }
}

if (! function_exists('image')) {
    /**
     * 获取图片地址
     *
     * @param  string  $file_name
     * @param  integer $width
     * @param  integer $height
     * @param  integer $mode
     * @return string
     */
    function image($file_name = null, $width = null, $height = null, $mode = 1)
    {
        $url = 'http://' . env('QINIU_DEFAULT_DOMAIN') . '/' . $file_name;

        if ($width || $height) {

            $url .= '?imageView2/' . $mode;

            if ($width) {
                $url .= '/w/' . $width;
            }

            if ($height) {
                $url .= '/h/' . $height;
            }
        }

        return $url;
    }
}


